import QtQuick 2.12
import QtQuick.Window 2.12

Window {
    id:root
    visible: true
    width: 640
    height: 480
    title: qsTr("MouseDrag")

    Rectangle {
        width: 200
        height: 200
        color: "green"

        Text {
            anchors.centerIn: parent
            color: "white"
            text: qsTr("drag me")
        }

        MouseArea {
            /* 注意：如果是要移动Window，则需要指定Window id */
            /* 在Window域下的parent:QQuickRootItem() root：QQuickWindowQmlImpl是不同的东西 */
            anchors.fill: parent
            property point clickPos: "1,1"

            onPressed: {
                clickPos = Qt.point(mouse.x,mouse.y)
            }

            onPositionChanged: {
                parent.x = mouse.x - clickPos.x + parent.x
                parent.y = mouse.y - clickPos.y + parent.y
            }
        }
    }
}
